package jataaka.parser;

import jataaka.value.DePaginasType;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Particular implementation of a parser service for the site DePaginas.com.ar
 * 
 * @author Juan Carlos Cancela
 * 
 */
public class DePaginasParserService extends BaseParserService implements
		ParserService {

	/**
	 * the current class name for the tag that begins containing the information taht needs to be parsed
	 */
	private static final String CLASS_FOR_TAG_THAT_CONTAINS_PLACE_INFO = "title fn org";

	/**
	 * retrieves the transformed information
	 */
	public ArrayList<HashMap<String, String>> retrieveTransformedInfo(String stringURL) {
		BufferedReader reader = null;
		ArrayList<HashMap<String, String>> transformedElements = new ArrayList<HashMap<String,String>>();
		try {
			reader = BaseParserService.createStreamForRead(stringURL);
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			String line = BaseParserService.retrieveNextLine(reader);
			while(line != null){
				if(this.isLineBeginOnParseableStructure(line)){
					transformedElements.add(createAndRetrieveParsedElement(reader, line));
				}
				line = BaseParserService.retrieveNextLine(reader);
			}
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return transformedElements;
	}

	/**
	 * creates a parsed element
	 * @param reader the reader stream
	 * @param line the line that contains the information
	 * @throws IOException
	 */
	private HashMap<String,String> createAndRetrieveParsedElement(BufferedReader reader, String line)
			throws IOException {
		HashMap<String,String> element = new HashMap<String,String>();
		element.put(DePaginasType.NAME.toString(), DePaginasParserService.extractInformationFromLine(line, DePaginasType.NAME));
		BaseParserService.retrieveNextLine(reader); //this additional call is required to step up a line
		element.put(DePaginasType.ADDRESS.toString(), DePaginasParserService.extractInformationFromLine(BaseParserService.retrieveNextLine(reader), DePaginasType.ADDRESS));
		element.put(DePaginasType.TYPE.toString(), DePaginasParserService.extractInformationFromLine(BaseParserService.retrieveNextLine(reader), DePaginasType.TYPE));
		element.put(DePaginasType.WEBSITE.toString(), DePaginasParserService.extractInformationFromLine(BaseParserService.retrieveNextLine(reader), DePaginasType.WEBSITE));
		
		return element;
	}

	/**
	 * determines if the line being analyzed is the beginnig of the parseable
	 * structure of the DePaginas code
	 */
	public boolean isLineBeginOnParseableStructure(String line) {
		if (line.contains(CLASS_FOR_TAG_THAT_CONTAINS_PLACE_INFO)) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * receives a line with xml and other not needed information, and retrieves only the data without it
	 * 
	 * @return cleaned information
	 */
	public static String extractInformationFromLine(String dirtyLine,
			DePaginasType infoType) {
		if (infoType.toString().equalsIgnoreCase("name") && dirtyLine != null && !dirtyLine.isEmpty()) {
			return dirtyLine.substring(dirtyLine.indexOf("'>")+2, dirtyLine.indexOf("</a>"));
		}
		if (infoType.toString().equalsIgnoreCase("type") && dirtyLine != null && !dirtyLine.isEmpty()) {
			return dirtyLine.substring(dirtyLine.indexOf(">")+1, dirtyLine.indexOf("</"));
		}
		if (infoType.toString().equalsIgnoreCase("address") && dirtyLine != null && !dirtyLine.isEmpty()) {
			return dirtyLine.substring(dirtyLine.indexOf(">")+1, dirtyLine.indexOf("</"));
		}
		if (infoType.toString().equalsIgnoreCase("website") && dirtyLine != null && !dirtyLine.isEmpty() && dirtyLine.contains("www.")) {
			return dirtyLine.substring(dirtyLine.indexOf(">")+1, dirtyLine.indexOf("</a>"));
		}
		return null;
	}

}
